#include "screen.h"
#include "pic.h"
#include "key.h"
#include "encoder.h"

typedef struct 
{
    /* data */
    char* name[15];         //组件名字
    const uint8_t *picture; //显示的图标指针50*50的图标
    uint16_t picture_x;
    uint16_t picture_y;
    uint16_t rect_x_start, rect_y_start;
    uint16_t rect_x_end, rect_y_end;    //背景框大小
    uint16_t value;         //数值
    uint16_t second_rect_offset_x;
}SCREEN_COMPONENT_PROPERTIES;


SCREEN_COMPONENT_PROPERTIES home_component_properties[2] = {
    {
        .name = "fan",
        .picture = fan,
        .picture_x = 20,
        .picture_y = 80,
        .rect_x_start = 100,
        .rect_y_start = 80,
        .rect_x_end = 180,
        .rect_y_end = 130,
        .second_rect_offset_x = 100
    },
    {
        .name = "temperature",
        .picture = temperature,
        .picture_x = 20,
        .picture_y = 150,
        .rect_x_start = 100,
        .rect_y_start = 150,
        .rect_x_end = 180,
        .rect_y_end = 200,
        .second_rect_offset_x = 100
    }
};


/**
 * 主屏幕home显示界面
 * 参数：flag：是否全屏刷新
*/
void screen_home(rt_bool_t flag)
{
    char buf[20] = {0};
    static int8_t fan_duty_temp = -1;
    static uint16_t fan_temperature_temp = 0;

    static int8_t fan_duty_target_temp = -1;
    static uint16_t fan_temperature_target_temp = 0;

    if(flag)
    {
        TFTLCD_Fill(0, 0, TFT_WIDTH, TFT_HIGHT, BLACK); 
        TFTLCD_Image(115, 10, 50, 50, disp);
        TFTLCD_Image(215, 10, 50, 50, setting);  
        TFTLCD_Image(home_component_properties[0].picture_x, home_component_properties[0].picture_y, 
                        50, 50, home_component_properties[0].picture);
        //画矩形
        // TFT_LCD_Drawarc_rectangle(home_component_properties[0].rect_x_start,
        //             home_component_properties[0].rect_y_start,
        //             home_component_properties[0].rect_x_end,
        //             home_component_properties[0].rect_y_end,
        //             10,
        //             BRED);

        TFTLCD_Image(home_component_properties[1].picture_x, home_component_properties[1].picture_y, 
                        50, 50, home_component_properties[1].picture);
        //画矩形
        // TFT_LCD_Drawarc_rectangle(home_component_properties[1].rect_x_start,
        //             home_component_properties[1].rect_y_start,
        //             home_component_properties[1].rect_x_end,
        //             home_component_properties[1].rect_y_end,
        //             10,
        //             CYAN);
    }
    // 刷新数值
    if (fan_duty_temp != fan_data.fan_duty)
    {
        rt_memset(buf, 0, sizeof(buf));
        fan_duty_temp = fan_data.fan_duty;
        rt_snprintf(buf, sizeof(buf), "%d", fan_data.fan_duty);
        TFTLCD_string_rect(home_component_properties[0].rect_x_start, 
                            home_component_properties[0].rect_y_start, 
                            home_component_properties[0].rect_x_end - home_component_properties[0].rect_x_start,
                            home_component_properties[0].rect_y_end - home_component_properties[0].rect_y_start,
                            10,
                            (uint8_t *)buf,
                            WHITE, 
                            BRED);
    }

    if (fan_temperature_temp != (int)fan_temp_getval())
    {
        fan_temperature_temp = (int)fan_temp_getval();
        // 风枪当前温度显示
        rt_memset(buf, 0, sizeof(buf));
        rt_snprintf(buf, sizeof(buf), "%d", (int)fan_temp_getval());
        TFTLCD_string_rect(home_component_properties[1].rect_x_start, 
                            home_component_properties[1].rect_y_start, 
                            home_component_properties[1].rect_x_end - home_component_properties[1].rect_x_start,
                            home_component_properties[1].rect_y_end - home_component_properties[1].rect_y_start,
                            10,
                            (uint8_t *)buf,
                            WHITE, 
                            BRED);    
    }
    /************************************************************************************************** */
    // 显示设定的目标值
    if (fan_duty_target_temp != fan_data.fan_duty_target)
    {
        rt_memset(buf, 0, sizeof(buf));
        fan_duty_target_temp = fan_data.fan_duty_target;
        rt_snprintf(buf, sizeof(buf), "%d", fan_data.fan_duty_target);
        TFTLCD_string_rect(home_component_properties[0].rect_x_start + home_component_properties[0].second_rect_offset_x, 
                            home_component_properties[0].rect_y_start, 
                            home_component_properties[0].rect_x_end - home_component_properties[0].rect_x_start,
                            home_component_properties[0].rect_y_end - home_component_properties[0].rect_y_start,
                            10,
                            (uint8_t *)buf,
                            WHITE, 
                            DARKBLUE);
    }

    if (fan_temperature_target_temp != fan_data.fan_temperature_target)
    {
        fan_temperature_target_temp = fan_data.fan_temperature_target;
        // 风枪当前温度显示
        rt_memset(buf, 0, sizeof(buf));
        rt_snprintf(buf, sizeof(buf), "%d", fan_data.fan_temperature_target);
        TFTLCD_string_rect(home_component_properties[1].rect_x_start + home_component_properties[1].second_rect_offset_x, 
                            home_component_properties[1].rect_y_start, 
                            home_component_properties[1].rect_x_end - home_component_properties[1].rect_x_start,
                            home_component_properties[1].rect_y_end - home_component_properties[1].rect_y_start,
                            10,
                            (uint8_t *)buf,
                            WHITE, 
                            DARKBLUE);    
    }
                
}


void screen_thread_entry(void *parameter)
{
    TFTLCD_Init();      //屏幕初始化
    // 创建画布
    TFTLCD_Fill(0, 0, TFT_WIDTH, TFT_HIGHT, BLACK);
    uint8_t key_cnt = 0;
    screen_home(RT_TRUE);
    while (1)
    {
        /* code */
        screen_home(RT_FALSE);
        if (rt_event_recv(key_event, key_b0_short_flag, RT_EVENT_FLAG_CLEAR|RT_EVENT_FLAG_OR, RT_IPC_FLAG_FIFO, RT_NULL) == RT_EOK)
        {
            if(++key_cnt >= 3) key_cnt=0;
        }
        rt_kprintf("%d\n", key_cnt);
        switch (key_cnt)
        {
            case 0:
                /* code */
                TFTLCD_DrawCircle(300, 
                                (home_component_properties[1].rect_y_end + home_component_properties[1].rect_y_start)/2,
                                8,
                                BLACK);  
                // 根据磁贴反馈开关热风枪
                if (HAL_GPIO_ReadPin(KEY_RS_GPIO_Port, KEY_RS_Pin) == GPIO_PIN_SET)
                {
                    fan_set_pwm(fan_data.fan_duty_target);
                    temperature_pid_control(fan_data.fan_temperature_target);
                }  
                else{
                    if((int)fan_temp_getval() > 200) fan_set_pwm(100);
                    else if((int)fan_temp_getval() < 130) fan_set_pwm(0);
                    temperature_pid_control(0);
                }     
                break;
            case 1:
                /* 设置风速 */
                fan_data.fan_duty_target = fan_data.fan_duty_target + encoder.cnt_speed;
                if(fan_data.fan_duty_target < 10) fan_data.fan_duty_target = 10;
                else if(fan_data.fan_duty_target > 100) fan_data.fan_duty_target = 100;
            
                TFTLCD_DrawCircle(300, 
                                (home_component_properties[0].rect_y_end + home_component_properties[0].rect_y_start)/2,
                                8,
                                GREEN);
                TFTLCD_DrawCircle(300, 
                                (home_component_properties[1].rect_y_end + home_component_properties[1].rect_y_start)/2,
                                8,
                                BLACK);                
                break;
            case 2:
                /* 设置温度 */
                fan_data.fan_temperature_target = fan_data.fan_temperature_target + encoder.cnt_speed;
                if(fan_data.fan_temperature_target < 25) fan_data.fan_temperature_target = 25;
                else if (fan_data.fan_temperature_target > 500) fan_data.fan_temperature_target = 500;

                TFTLCD_DrawCircle(300, 
                                (home_component_properties[0].rect_y_end + home_component_properties[0].rect_y_start)/2,
                                8,
                                BLACK);
                TFTLCD_DrawCircle(300, 
                                (home_component_properties[1].rect_y_end + home_component_properties[1].rect_y_start)/2,
                                8,
                                GREEN);
                break;
            default:
                break;
        }
        rt_thread_mdelay(100);
    }
    
}

